package com.kexio.websocket.dto;

import com.fasterxml.jackson.annotation.JsonInclude;

/**
 * WebSocket 消息数据传输对象
 * 
 * 定义 WebSocket 消息的统一格式。
 * 
 * @author Kexio Team
 */
@JsonInclude(JsonInclude.Include.NON_NULL)
public class WebSocketMessage<T> {

    /**
     * 消息类型
     * - system: 系统消息
     * - notification: 通知消息
     * - message: 私信消息
     * - ping: 心跳检测
     * - pong: 心跳响应
     * - typing: 正在输入状态
     * - user_status: 用户状态变更
     * - error: 错误消息
     */
    private String type;

    /**
     * 消息内容
     */
    private String content;

    /**
     * 消息数据（具体业务数据）
     */
    private T data;

    /**
     * 发送者ID
     */
    private String senderId;

    /**
     * 接收者ID
     */
    private String receiverId;

    /**
     * 目标类型（post/comment/user等）
     */
    private String targetType;

    /**
     * 目标ID
     */
    private String targetId;

    /**
     * 消息时间戳
     */
    private Long timestamp;

    /**
     * 消息ID（可选）
     */
    private String messageId;

    /**
     * 扩展属性
     */
    private Object extra;

    // 构造方法
    public WebSocketMessage() {}

    public WebSocketMessage(String type, String content, T data, String senderId, String receiverId, 
                           String targetType, String targetId, Long timestamp, String messageId, Object extra) {
        this.type = type;
        this.content = content;
        this.data = data;
        this.senderId = senderId;
        this.receiverId = receiverId;
        this.targetType = targetType;
        this.targetId = targetId;
        this.timestamp = timestamp;
        this.messageId = messageId;
        this.extra = extra;
    }

    // Getter 和 Setter 方法
    public String getType() {
        return type;
    }

    public void setType(String type) {
        this.type = type;
    }

    public String getContent() {
        return content;
    }

    public void setContent(String content) {
        this.content = content;
    }

    public T getData() {
        return data;
    }

    public void setData(T data) {
        this.data = data;
    }

    public String getSenderId() {
        return senderId;
    }

    public void setSenderId(String senderId) {
        this.senderId = senderId;
    }

    public String getReceiverId() {
        return receiverId;
    }

    public void setReceiverId(String receiverId) {
        this.receiverId = receiverId;
    }

    public String getTargetType() {
        return targetType;
    }

    public void setTargetType(String targetType) {
        this.targetType = targetType;
    }

    public String getTargetId() {
        return targetId;
    }

    public void setTargetId(String targetId) {
        this.targetId = targetId;
    }

    public Long getTimestamp() {
        return timestamp;
    }

    public void setTimestamp(Long timestamp) {
        this.timestamp = timestamp;
    }

    public String getMessageId() {
        return messageId;
    }

    public void setMessageId(String messageId) {
        this.messageId = messageId;
    }

    public Object getExtra() {
        return extra;
    }

    public void setExtra(Object extra) {
        this.extra = extra;
    }

    /**
     * 创建系统消息
     */
    public static WebSocketMessage<String> systemMessage(String content) {
        WebSocketMessage<String> message = new WebSocketMessage<>();
        message.setType("system");
        message.setContent(content);
        message.setTimestamp(System.currentTimeMillis());
        return message;
    }

    /**
     * 创建通知消息
     */
    public static <T> WebSocketMessage<T> notificationMessage(String content, T data) {
        WebSocketMessage<T> message = new WebSocketMessage<>();
        message.setType("notification");
        message.setContent(content);
        message.setData(data);
        message.setTimestamp(System.currentTimeMillis());
        return message;
    }

    /**
     * 创建私信消息
     */
    public static <T> WebSocketMessage<T> privateMessage(String senderId, String receiverId, String content, T data) {
        WebSocketMessage<T> message = new WebSocketMessage<>();
        message.setType("message");
        message.setSenderId(senderId);
        message.setReceiverId(receiverId);
        message.setContent(content);
        message.setData(data);
        message.setTimestamp(System.currentTimeMillis());
        return message;
    }

    /**
     * 创建错误消息
     */
    public static WebSocketMessage<String> errorMessage(String error) {
        WebSocketMessage<String> message = new WebSocketMessage<>();
        message.setType("error");
        message.setContent(error);
        message.setTimestamp(System.currentTimeMillis());
        return message;
    }

    /**
     * 创建心跳消息
     */
    public static WebSocketMessage<Void> pingMessage() {
        WebSocketMessage<Void> message = new WebSocketMessage<>();
        message.setType("ping");
        message.setTimestamp(System.currentTimeMillis());
        return message;
    }

    /**
     * 创建心跳响应
     */
    public static WebSocketMessage<Void> pongMessage() {
        WebSocketMessage<Void> message = new WebSocketMessage<>();
        message.setType("pong");
        message.setTimestamp(System.currentTimeMillis());
        return message;
    }
}
